/**
 * FileSystemStoreNode - a store node on a filesystem
 *
 * @author Scot McSweeney-Roberts
 * @version 0.1
 *
 */

package uk.co.sheitgeist.fums.server.content.store.filesystem;

import uk.co.sheitgeist.fums.server.content.store.*;
import java.util.logging.Logger;
import javax.activation.MimetypesFileTypeMap;
import javax.activation.MimeType

public class FileSystemStoreNode implements StoreNode {
	private static Logger logger = Logger.getLogger("uk.co.sheitgeist")
	private MimetypesFileTypeMap mimetypes = new MimetypesFileTypeMap();
	
	private File file;
	
	public String getId() {
		return file.toString();
	}

	public String getName() {
		return file.name
	}

	public String getContentType() {
		return mimetypes.getContentType(this.file)
	}

	public String getPrimaryType() {
		return new MimeType(this.getContentType()).getPrimaryType()
	}

	public String getSubType() {
		return new MimeType(this.getContentType()).getSubType()
	}

	public FileSystemStoreNode(File file) {
		this.file = file
		//add in some mimetypes
		mimetypes.addMimeTypes("audio/mpeg mp3 MP3")
		mimetypes.addMimeTypes("video/mp4 mp4 MP4")
		mimetypes.addMimeTypes("image/jpeg jpeg jpg JPEG JPG")
	}

	public Boolean getIsFolder() {
		return this.file.isDirectory()
	}

	public List<StoreNode> getChildren() {
		logger.info "file: ${file.toString()}"
		List<StoreNode> nodes = new ArrayList<StoreNode>()
		file.eachFile {child ->
			logger.info child.toString()
			nodes.add(new FileSystemStoreNode(child))
		}
		return nodes
	}
}
